<?php

namespace app\modules\models;

use Yii;

/**
 * /**
 * 图片分组model
 * @author 黄宇翔
 * @access public
 * @since 1.0
 */
class ImagesGroup extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'images_group';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'app_id', 'cooperate_app_id'], 'required'],
            [['total', 'create_time'], 'integer'],
            [['id', 'app_id', 'cooperate_app_id'], 'string', 'max' => 36],
            [['name'], 'string', 'max' => 20]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'app_id' => 'App ID',
            'cooperate_app_id' => 'Cooperate App ID',
            'name' => 'Name',
            'total' => 'Total',
            'create_time' => 'Create Time',
        ];
    }

    /**
     * 创建分组
     * @author 黄宇翔
     * @access public
     * @param mixed $id 分组id
     * @param mixed $appId 应用id
     * @param mixed $name 分组名字
     * @since 1.0
     * @return array
     */
    public function createGroup($id, $appId, $name)
    {
        return imagesGroup::find()->createCommand()->insert('images_group', array(
            'id' => $id,
            'app_id' => $appId,
            'name' => $name,
            'total' => 0,
            'create_time' => time()
        ))->execute();
    }

    /**
     * 更新分组
     * @author 黄宇翔
     * @access public
     * @param mixed $name 分组名字
     * @param mixed $groupId 分组id
     * @since 1.0
     * @return string
     */
    public function updateGroup($name, $groupId)
    {
        return imagesGroup::find()->createCommand()->update('images_group', array(
            'name' => $name
        ), "id='$groupId'")->execute();
    }

    /**
     * 删除用户分组
     * @author 黄宇翔
     * @access public
     * @param mixed $id 用户id
     * @since 1.0
     * @return Boolean
     */
    public function deleteGroup($id, $appId)
    {
        $return = $this::find()->createCommand()->delete('images_group', ['id' => $id, 'app_id' => $appId])->execute();
        if ($return) {
            return TRUE;
        }
        return FALSE;

    }

    /**
     * total加1
     * @author 黄宇翔
     * @access public
     * @param mixed $groupId 分组id
     * @since 1.0
     * @return array
     */
    public function addTotal($groupId)
    {
		 $sql="update images_group set total=total+1 where id='$groupId'";
        return Yii::$app->db->createCommand($sql)->execute();
    }
	/**
     * total-1
     * @author 黄宇翔
     * @access public
     * @param mixed $groupId 分组id
     * @since 1.0
     * @return array
     */
    public function redTotal($groupId)
    {
		 $sql="update images_group set total=total-1 where id='$groupId'";
        return Yii::$app->db->createCommand($sql)->execute();
    }


    /**
     * 更新分组图片数量
     * @author 黄宇翔
     * @access public
     * @param mixed $total 图片数量
     * @param mixed $groupId 图片id
     * @since 1.0
     * @return array
     */
    public function updateGroupTotal($total, $groupId)
    {
        return imagesgroup::find()->createCommand()->update('images_group', array(
            'total' => $total
        ), "id='$groupId'")->execute();
    }
    /**
     *按照分页读取信息
     * @author 黄宇翔
     * @param mixed $appId 应用id
     * @param mixed $limit 分页数
     * @param mixed $pageno 分页数
     * @access public
     * @since 1.0
     * @return boolean
     */
    public function findByItems($appId,$limit,$pageno){
        $storeCount = $this::find()->where(['app_id' => $appId])->count('id');
        $pageno = ($pageno - 1) * $limit;
        $data['items'] = $this::find()->where(['app_id' => $appId])->limit($limit)->offset($pageno)->orderBy('create_time')->all();
        $data['pageno'] = ceil($storeCount / $limit);
        $data['total'] = $storeCount;
        return $data;
    }

}
